package com.spider.admin.agent;

import com.spider.admin.conf.ENVConf;
import com.spider.admin.utils.FileUtils;
import com.spider.admin.utils.OSCommandUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
@Slf4j
public class PublishWeb {

    @Autowired
    private ENVConf envConf;

    public void publishWebPage() {
        try {
            String webDirectoryPath = envConf.getUiContainerBasePath();
            if (OSCommandUtil.isWinOS()) {
                webDirectoryPath = webDirectoryPath;
            } else {
                webDirectoryPath = webDirectoryPath + "/nginx";
            }
            if (new File(webDirectoryPath + "/" + envConf.getWebDirectory()).exists()) {
                FileUtils.deleteDirectoryRecursively(webDirectoryPath + "/" + envConf.getWebDirectory());
            }
            String srcFilePath = Thread.currentThread().getContextClassLoader().getResource("").getPath()
                    + "html";
            FileUtils.copyFolder(new File(srcFilePath), new File(webDirectoryPath), envConf.getWebDirectory());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
